package com.kdgame.ggtest.unitylib;

import android.content.Intent;
import android.util.Log;

import com.thomas.gamesdk.ThomasGameSdk;
import com.thomas.gamesdk.adapter.AbsRewardedVideoListener;
import com.thomas.gamesdk.open.LoginUserInfo;
import com.thomas.gamesdk.open.SDKCode;
import com.thomas.gamesdk.open.SDKPayListener;
import com.thomas.gamesdk.open.SdkCallbackListener;
import com.thomas.gamesdk.open.SdkLoginListener;
import com.thomas.gamesdk.open.SdkPayInfo;

public class MainActivity extends BaseUnityActivity {

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        ThomasGameSdk.getInstance().onActivityResult(getUnityActivity(), requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }


    /**
     * 初始化
     */
    @Override
    public void onHandlerInitMessage() {
        super.onHandlerInitMessage();

        //开启日志
        ThomasGameSdk.getInstance().thomasOpenLog(true);
        //启用测试服
        ThomasGameSdk.getInstance().thomasUseTestServer();
        //登录回调
        ThomasGameSdk.getInstance().thomasSetSdkLoginListener(new SdkLoginListener() {
            @Override
            public void onLogin(int code, LoginUserInfo user) {
                if (code == SDKCode.SUCCESS) {
                    Log.e("11111", "onLogin success");
                    sendLoginSuccess(user.getUserID(), user.getSession());
                } else if (code == SDKCode.FAILURE) {
                    Log.e("11111", "onLogin failure");
                    sendLoginFail(user.getErrorMsg());
                } else if (code == SDKCode.CANCLE) {
                    Log.e("11111", "onLogin cancel");
                    sendLoginFail("用户取消登录");
                }
            }

            @Override
            public void onLogout(int code, String msg) {
                if (code == SDKCode.SUCCESS) {
                    Log.e("11111", "注销成功");
                } else if (code == SDKCode.FAILURE) {
                    Log.e("11111", "注销失败" + msg);
                }
            }
        });

        //SDK初始化
        ThomasGameSdk.getInstance().thomasSdkInit(getUnityActivity(), new SdkCallbackListener<String>() {
            @Override
            public void callback(int code, String response) {
                switch (code) {
                    case SDKCode.SUCCESS:
                        Log.e("11111", "init success");
                        sendInitSuccess();
                        break;
                    case SDKCode.FAILURE:
                        Log.e("11111", "init failure");
                        sendInitFail();
                        break;
                }
            }
        });
    }

    @Override
    public void onHandlerLoginMessage() {
        super.onHandlerLoginMessage();
        ThomasGameSdk.getInstance().thomasSdkLogin(getUnityActivity());
    }

    @Override
    public void onHandlerLogoutMessage() {
        super.onHandlerLogoutMessage();
        ThomasGameSdk.getInstance().thomasSdkSwichLogin(getUnityActivity());
    }

    @Override
    public void onHandlerPayMessage(String cpOrderSN) {
        super.onHandlerPayMessage(cpOrderSN);

        SdkPayInfo payInfo = new SdkPayInfo();
        payInfo.setCpOrderSn(cpOrderSN);
        payInfo.setGoodsName("测试商品$0.99");
        payInfo.setSkuId("android.test.purchased");
        payInfo.setGameServer("101");
        payInfo.setRoleName("role102");
        payInfo.setRoleId("103");
        payInfo.setExt("透传字段");
        ThomasGameSdk.getInstance().thomasSdkPay(this, payInfo, new SDKPayListener() {
            @Override
            public void onPayFinish() {
                Log.e("11111", "onPayFinish");
                sendPaySuccess();
            }

            @Override
            public void onPayFailed(String msg) {
                Log.e("11111", "onPayFailed");
                sendPayFail();
            }
        });
    }

    @Override
    public void onHandlerUserCenter() {
        super.onHandlerUserCenter();
        ThomasGameSdk.getInstance().thomasOpenUserCenter(getUnityActivity());
    }

    @Override
    public void onHandlerRequestExitMessage() {
        super.onHandlerRequestExitMessage();
        ThomasGameSdk.getInstance().thomasExitGame(getUnityActivity(), new SdkCallbackListener<String>() {
            @Override
            public void callback(int i, String s) {

            }
        });
    }


    /**
     * 广告SDK
     */

    private boolean hasInitAD = false;

    @Override
    public void onHandlerPlayAd() {
        super.onHandlerPlayAd();

        if (hasInitAD) {
            ThomasGameSdk.getInstance().thomasAdShowRewardedVideo("RareChest");
        } else {

            hasInitAD = true;
            ThomasGameSdk.getInstance().thomasAdEnable(this);
            ThomasGameSdk.getInstance().thomasAdValidateIntegration(this);
            ThomasGameSdk.getInstance().thomasAdSetRewardedVideoListener(new AbsRewardedVideoListener() {
                @Override
                public void onRewardedVideoAdOpened() {
                    super.onRewardedVideoAdOpened();
                }

                @Override
                public void onRewardedVideoAdClosed() {
                    super.onRewardedVideoAdClosed();
                }

                @Override
                public void onRewardedVideoAvailabilityChanged(boolean b) {
                    super.onRewardedVideoAvailabilityChanged(b);
                    ThomasGameSdk.getInstance().thomasAdShowRewardedVideo();
                }

                @Override
                public void onRewardedVideoAdStarted() {
                    super.onRewardedVideoAdStarted();
                }

                @Override
                public void onRewardedVideoAdEnded() {
                    super.onRewardedVideoAdEnded();
                }

                @Override
                public void onRewardedVideoAdRewarded(com.ironsource.mediationsdk.model.Placement placement) {
                    super.onRewardedVideoAdRewarded(placement);
                }

                @Override
                public void onRewardedVideoAdShowFailed(com.ironsource.mediationsdk.logger.IronSourceError ironSourceError) {
                    super.onRewardedVideoAdShowFailed(ironSourceError);
                }

                @Override
                public void onRewardedVideoAdClicked(com.ironsource.mediationsdk.model.Placement placement) {
                    super.onRewardedVideoAdClicked(placement);
                }
            });
        }
    }
}
